package com.happy.unitconvert.model.enums;

/**
 * 单位类型枚举
 */
public enum UnitType {

  LENGTH("length", "长度"),
  WEIGHT("weight", "重量"),
  TEMPERATURE("temperature", "温度"),
  AREA("area", "面积"),
  VOLUME("volume", "体积"),
  PRESSURE("pressure", "压力"),
  SPEED("speed", "速度"),
  TIME("time", "时间"),
  ENERGY("energy", "能量"),
  POWER("power", "功率");

  private final String code;
  private final String description;

  UnitType(String code, String description) {
    this.code = code;
    this.description = description;
  }

  public String getCode() {
    return code;
  }

  public String getDescription() {
    return description;
  }

  /**
   * 根据code获取对应的UnitType
   */
  public static UnitType fromCode(String code) {
    for (UnitType unitType : values()) {
      if (unitType.getCode().equals(code)) {
        return unitType;
      }
    }
    throw new IllegalArgumentException("不支持的单位类型: " + code);
  }

  /**
   * 判断是否为有效的单位类型code
   */
  public static boolean isValidCode(String code) {
    try {
      fromCode(code);
      return true;
    } catch (IllegalArgumentException e) {
      return false;
    }
  }
}
